package com.expert.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.expert.Mapper.ExtractAvoidMapper;
import com.expert.context.BaseContext;
import com.expert.dto.ExtractAvoidInfoDTO;
import com.expert.exception.AddException;
import com.expert.pojo.Expert;
import com.expert.pojo.ExtractAvoid;
import com.expert.pojo.ExtractCondition;
import com.expert.service.ExpertService;
import com.expert.service.ExtractAvoidService;
import com.expert.service.ExtractConditionService;
import com.expert.vo.ExtractAvoidInitVo;
import com.expert.vo.RemoteExpertVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;

/**
 * @ClassName: ExtractAvoidServiceImpl
 * @Author: cxs
 * @Date: 2024/03/22 16:43
 * @Description:
 **/

@Service
public class ExtractAvoidServiceImpl extends ServiceImpl<ExtractAvoidMapper,ExtractAvoid> implements ExtractAvoidService {

    @Autowired
    ExtractConditionService extractConditionService;

    @Autowired
    ExpertService expertService;
    /**
     * 初始化回避消息
     *
     * @param id
     * @return
     */
    @Override
    public ExtractAvoidInitVo initAvoid(Long id) {
        List<ExtractAvoid> extractAvoidList = this.lambdaQuery().eq(ExtractAvoid::getProjectId, id).list();
        ExtractAvoidInitVo avoidInitVo=new ExtractAvoidInitVo();
        if (extractAvoidList.size()>0){
            avoidInitVo.setHasData(true);
        }
        List<RemoteExpertVo> expertVos=new ArrayList<>();
        StringJoiner unitJoiner=new StringJoiner("；");
        StringJoiner regionJoiner=new StringJoiner("；");
        extractAvoidList.forEach(item->{
            if (item.getAvoidDefaultExpert()!=null){
                RemoteExpertVo remoteExpertVo=new RemoteExpertVo();
                remoteExpertVo.setId(item.getAvoidDefaultExpert());
                remoteExpertVo.setName(item.getAvoidDefaultExpertName());
                remoteExpertVo.setIdNumber(item.getAvoidDefaultExpertIdNumber().substring(12));
                expertVos.add(remoteExpertVo);
            }
            if (item.getAvoidUnit()!=null){
                unitJoiner.add(item.getAvoidUnit());
            }
            if (item.getAvoidRegion()!=null){
                regionJoiner.add(item.getAvoidRegion());
            }
        });
        if (regionJoiner.length()>0){
            avoidInitVo.setAvoidRegion(regionJoiner.toString());
        }
        if (unitJoiner.length()>0){
            avoidInitVo.setAvoidUnit(unitJoiner.toString());
        }
        System.out.println(unitJoiner.toString());
        avoidInitVo.setDefaultExpert(expertVos);
        return avoidInitVo;
    }

    /**
     * 添加回避条件信息
     *
     * @param extractAvoidInfoDTO
     */
    @Override
    @Transactional
    public void addAvoid(ExtractAvoidInfoDTO extractAvoidInfoDTO) {
        List<Long> extractDefaultExpert = extractAvoidInfoDTO.getExtractDefaultExpert();
        Long projectId = extractAvoidInfoDTO.getProjectId();
        List<ExtractAvoid> extractAvoidList = this.lambdaQuery().eq(ExtractAvoid::getProjectId, projectId).list();
        this.removeBatchByIds(extractAvoidList);
        List<ExtractCondition> list = this.extractConditionService.lambdaQuery().eq(ExtractCondition::getProjectId, projectId).and(extractConditionLambdaQueryWrapper -> {
            extractConditionLambdaQueryWrapper.isNotNull(ExtractCondition::getExtractDefaultExpert);
        }).list();
        List<Long> collect = list.stream().map(ExtractCondition::getExtractDefaultExpert).toList();
        int size = CollectionUtils.intersection(collect, extractDefaultExpert).size();
        if (size>0){
            throw  new AddException("回避专家信息中存在指定评审专家");
        }
        ArrayList<ExtractAvoid>  extractAvoids=new ArrayList<>();
        //添加指定回避专家
        extractDefaultExpert.forEach(expert->{
            Expert expertInfo = this.expertService.getById(expert);
            ExtractAvoid extractAvoid=new ExtractAvoid();
            extractAvoid.setAvoidDefaultExpert(expert);
            extractAvoid.setAvoidDefaultExpertIdNumber(expertInfo.getExpertIdNumber());
            extractAvoid.setAvoidDefaultExpertName(expertInfo.getExpertName());
            extractAvoid.setCreateUser(BaseContext.getCurrentId());
            extractAvoid.setCreateTime(LocalDateTime.now());
            extractAvoid.setUpdateTime(LocalDateTime.now());
            extractAvoid.setUpdateUser(BaseContext.getCurrentId());
            extractAvoid.setProjectId(projectId);
            extractAvoids.add(extractAvoid);
        });
        String avoidRegion = extractAvoidInfoDTO.getAvoidRegion();
        if (extractAvoidInfoDTO.getAvoidUnit()!=null){
            ArrayList<String> unitList=new ArrayList<>(Arrays.asList(extractAvoidInfoDTO.getAvoidUnit().split("；")));
            unitList.forEach(unit->{
                ExtractAvoid extractAvoid=new ExtractAvoid();
                extractAvoid.setAvoidUnit(unit);
                extractAvoid.setCreateTime(LocalDateTime.now());
                extractAvoid.setUpdateTime(LocalDateTime.now());
                extractAvoid.setUpdateUser(BaseContext.getCurrentId());
                extractAvoid.setProjectId(projectId);
                extractAvoids.add(extractAvoid);
            });
        }
        if (extractAvoidInfoDTO.getAvoidRegion()!=null){
            ArrayList<String> regionList = new ArrayList<>(Arrays.asList(avoidRegion.split("；")));
            regionList.forEach(region->{
                ExtractAvoid extractAvoid=new ExtractAvoid();
                extractAvoid.setAvoidRegion(region);
                extractAvoid.setCreateUser(BaseContext.getCurrentId());
                extractAvoid.setCreateTime(LocalDateTime.now());
                extractAvoid.setUpdateTime(LocalDateTime.now());
                extractAvoid.setUpdateUser(BaseContext.getCurrentId());
                extractAvoid.setProjectId(projectId);
                extractAvoids.add(extractAvoid);
            });
        }
        this.saveBatch(extractAvoids);
    }
}
